package clientMain;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;

import uploadServer.UploadImg;
import utility.TimeServer;
import utility.UtilDelegate;
import clientUtilGUI.ClosableTabbedPane;
import clientUtilGUI.UtilityGUI;
import clientView.Company_View;
import clientView.Employee_Add;
import database.CompanyDelegate;
import database.DepartmentDelegate;
import database.EmployeeDelegate;
import database.SBDelegate;
import database.SSDelegate;
import database.TimeSheetDelegate;
import database.ViewDepDelegate;
import database.ViewEmDelegate;

public  class Main extends JFrame{
	
	// record: hien thi so ban ghi trong 1 trang	
	public static int record = 15;
	public static int comId = 2;
	public static int userId = Login.listAuthorization.get(0).getUserId();
	
	public static ClosableTabbedPane jTabbedPane;
	
	public static String strFormatDate = "dd/MM/yyyy";
	public static SimpleDateFormat sdfDate = new SimpleDateFormat(strFormatDate);
	public static String strFormatTime = "kk:mm";/*hh:mm a*/
	public static SimpleDateFormat sdfTime = new SimpleDateFormat(strFormatTime);
	public static Date fullday = TimeServer.toDate(TimeServer.getTimeServer());
	public static String strDate = sdfDate.format(fullday);
	public static String strTime = sdfTime.format(fullday);
	

	public static Main main;
//	Database Delegate
	public static CompanyDelegate viewCompany;
	public static EmployeeDelegate actionEm;
	public static ViewEmDelegate viewEm;
	public static ViewDepDelegate viewDep;
	public static DepartmentDelegate actionDep;
	public static TimeSheetDelegate actionTS;
	public static SBDelegate actionSB;
	public static SSDelegate actionSS;
	

	/**
	 * Create the frame.
	 * @param name 
	 */
	public Main(String userName) {
//		Database Delegate
		viewCompany = new CompanyDelegate();
		actionEm = new EmployeeDelegate();
		viewEm = new ViewEmDelegate();
		actionDep = new DepartmentDelegate();
		viewDep = new ViewDepDelegate();
		actionTS = new TimeSheetDelegate();
		actionSB = new SBDelegate();
		actionSS = new SSDelegate();
		
		UtilityGUI.propertyFrameMain(this);
		jTabbedPane = new ClosableTabbedPane() {
			public boolean tabAboutToClose(int tabIndex) {
				String tab = jTabbedPane.getTabTitleAt(tabIndex);
				int choice = JOptionPane.showConfirmDialog(null, UtilityGUI.exitTabbedPane(tab), 
						UtilityGUI.titleAlert, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
				return choice == 0;
			}
		};
		jTabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		jTabbedPane.addTab(new Company_View(), UtilityGUI.tabComView);
		getContentPane().add(jTabbedPane, BorderLayout.CENTER);
		addWindowListener(new WindowEventHandler());
	}
		
	private class WindowEventHandler extends WindowAdapter{
		public void windowClosing(WindowEvent evt) {
			int option = JOptionPane.showConfirmDialog(null, UtilityGUI.exitApp, 
					UtilityGUI.titleAlert, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			if(option == JOptionPane.YES_OPTION){
				if(!(Employee_Add.fileName.equals(null) || Employee_Add.fileName.isEmpty())){
					boolean flag = UploadImg.delete(Employee_Add.fileName);
					if(flag == true){
						Employee_Add.fileName = "";			
					}else{
						JOptionPane.showMessageDialog(null, UtilityGUI.errorAction, 
										UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
						return;
					}
				}
				UtilDelegate.closeConnection(Login.conn);
			    System.exit(0);	
		    }else return;
		  }
	}
}